#include <iostream>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char* argv[])
{
    int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in local;
    bzero(&local, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = htons(atoi(argv[1]));
    local.sin_addr.s_addr = INADDR_ANY;
    if(bind(listen_fd, (sockaddr*)&local, sizeof(local)) == -1)
    {
        std::cout << "error: bind error!" << std::endl;
        exit(1); 
    }

    std::cout << "bind success!" << std::endl;

    if(listen(listen_fd, 5) == -1)
    {
        std::cout << "error: listen error!" << std::endl;
        exit(1);
    }
    
    struct sockaddr_in peer;
    socklen_t len = sizeof(peer);
    int fd = accept(listen_fd, (struct sockaddr*)&peer, &len);
    if(fd == -1)
    {
        std::cout << "error: accept error!" << std::endl;
        exit(1);
    }

    std::cout << "accept success!" << std::endl;

    // 绑定成功死循环
    while(true);

    return 0;
}
